﻿<%@ Page Title="" Language="C#" MasterPageFile="~/ZFMaster.Master" AutoEventWireup="true"
 CodeBehind="_c1.aspx.cs" Inherits="ZF.WS.test._c1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">

<script>
    var i_keep = 3;
    function _keep() {
        i_keep--
        if (i_keep == 0) {
            i_keep = 30;
            setTimeout("_keep()", 1000);
            $.ajax({
                type: "POST",
                data: String.format("{memberId:{0}}", memberId),
                contentType: "application/json",
                url: "/WSChat.asmx/Keep",
                cache: false,
                async: false,
                success: function(msg) {
                    var msg = $.evalJSON(msg).d;
                    if (!msg) {
                         
                    }
                }
            });
        } else
            setTimeout("_keep()", 1000);

    }
    $(function() {
        _keep();
         
        $(window).unload(function() {
           
            $.ajax({
                type: "POST",
                data: $.toJSON({ memberId: memberId, roomKey: RoomKey }),
                contentType: "application/json",
                url: "/WSChat.asmx/Out",
                cache: false,
                async: false,
                success: function(msg) {
                    var msg = $.evalJSON(msg).d;
                }
            });
        });
    });
    
    
    
</script>

</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <link href="/fckeditor/_samples/sample.css" rel="stylesheet" type="text/css" />
     <script type="text/javascript" src="/fckeditor/fckeditor.js"></script>
     
     <style>
        #br   { width: 100%; height: 300px; background: #fafafa;  border:1px solid #3399FF; overflow:scroll; }
        #br dl { margin-bottom: 5px; border-bottom: 1px solid #eee; margin-left: 5px; }
        #br dl dt{text-align:left;}
        #br dl dt.dt1 { color: #008040; }
        #br dl dt.dt2 { color: #44B8FB; }
        #br dl dd { margin-left: 10px; margin-top: 5px; text-align:left; }
        #br dl dd img { margin: 1px; }
        #br dl dd.sys{ color:#f00; font-size:14px; }
    </style>
    
    <div id="br" >
    </div>
     <script type="text/javascript">
         var sBasePath = "/fckeditor/"
         var oFCKeditor = new FCKeditor('FCKeditor1');
         oFCKeditor.BasePath = sBasePath;
         oFCKeditor.ToolbarSet = 'Chat';
         oFCKeditor.Value = '';
         oFCKeditor.Create();
 
         var memberId=<%=this.CurrentUser.MemberId %>;
    </script>
     
    <script type="text/javascript">
        var RoomKey = "<%=this.RoomKey %>";
        var editor = null;
        function FCKeditor_OnComplete(editorInstance) {
            ok();
            editor = FCKeditorAPI.GetInstance("FCKeditor1");
            _rec();
        }
        
        function ok() {
            $("#send").click(function() {
                var html = editor.EditorDocument.body.innerHTML;
                _send(html);
                editor.EditorDocument.body.innerHTML = "";
            });
            
        }
        function gettime() {
            var today = new Date();
            var h = today.getHours();
            var m = today.getMinutes();
            var s = today.getSeconds();
            return String.format("{0}:{1}:{2}", h, m, s);
        }
        function _send(msg) {
            //        Send(int memberId, string msg, string roomKey)
            var data = { memberId: memberId, msg: msg, roomKey: RoomKey };
   
            $.ajax({
                type: "POST",
                data: $.toJSON(data),
                contentType: "application/json",
                url: "/WSChat.asmx/Send",
                cache: false,
                async: true,
                success: function(msg) {
                    msg = $.evalJSON(msg).d;
                    if (msg != null) {
                        if (msg.IType == 1) {
                            $("#br").append(String.format("<dl><dt class='dt1'>{1}&nbsp;&nbsp;&nbsp;&nbsp;{2}</dt><dd>{0}</dd></dl>",
                         msg.Msg, msg.FromMember, gettime()));
                        } else {
                            $("#br").append(String.format("<dl><dd class='sys'>系统消息：{0}</dd></dl>",
                             msg.Msg, msg.FromMember, gettime()));

                        }
                        _setDivScrollTop();
                    }

                }
            });
        }
        function _rec() {
            setTimeout("_rec()", 1000);

            $.ajax({
                type: "POST",
                data: String.format("{memberId:{0},roomKey:'{1}'}", memberId, RoomKey),
                contentType: "application/json",
                url: "/WSChat.asmx/Rec",
                cache: false,
                async: true,
                success: function(msg) {
                    var str = msg;
                    var msg = $.evalJSON(msg).d;
                    if (msg.length > 0) {
                        
                        $(msg).each(function() {
                            if (this.IType == 1) {
                                $("#br").append(String.format("<dl><dt class='dt2'>{1}&nbsp;&nbsp;&nbsp;&nbsp;{2}</dt><dd>{0}</dd></dl>",
                             this.Msg, this.FromMember, gettime()));
                            } else {
                                $("#br").append(String.format("<dl><dd class='sys'>系统消息：{0}</dd></dl>",
                             this.Msg, this.FromMember, gettime()));

                            }
                            _setDivScrollTop();
                        });
                    }
                }
            });
        }
        function _setDivScrollTop() {
            $("#br")[0].scrollTop = $("#br")[0].scrollHeight;
        }
    </script>
    
     
    
    <input type="button" id="send" value="s" />
    
    
    
    
    
</asp:Content>
